package com.mall.product.controller;

import com.mall.common.annotation.LogOperation;
import com.mall.common.utils.Constant;
import com.mall.common.page.PageData;
import com.mall.common.utils.ExcelUtils;
import com.mall.common.utils.Result;
import com.mall.common.validator.AssertUtils;
import com.mall.common.validator.ValidatorUtils;
import com.mall.common.validator.group.AddGroup;
import com.mall.common.validator.group.DefaultGroup;
import com.mall.common.validator.group.UpdateGroup;
import com.mall.product.dto.CategoryDTO;
import com.mall.product.excel.CategoryExcel;
import com.mall.product.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 商品三级分类
 *
 * @author xjc xjc@163.com
 * @since 1.0.0 2022-07-17
 */
@RestController
@RequestMapping("product/category")
@Api(tags="商品三级分类")
@Slf4j
public class CategoryController {

    @Resource
    private CategoryService categoryService;


    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true,dataTypeClass = Integer.class) ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true,dataTypeClass = Integer.class) ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataTypeClass = String.class) ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataTypeClass = String.class)
    })
    public Result<PageData<CategoryDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<CategoryDTO> page = categoryService.page(params);

        return new Result<PageData<CategoryDTO>>().ok(page);
    }

    @GetMapping("Tree")
    @ApiOperation("Tree")
    public Result<List<CategoryDTO>> TreeList(){
        List<CategoryDTO> list = categoryService.treeList();

        return new Result<List<CategoryDTO>>().ok(list);
    }


    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<CategoryDTO> get(@PathVariable("id") Long id){
        CategoryDTO data = categoryService.get(id);

        return new Result<CategoryDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    public Result save(@RequestBody CategoryDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        categoryService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    public Result update(@RequestBody CategoryDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        // 级联更新
        categoryService.updateCascade(dto);
//        categoryService.update(dto);

        return new Result();
    }

    @PostMapping("/categories")
    @ApiOperation("修改")
    @LogOperation("修改")
    public Result updates(@RequestBody List<CategoryDTO> list){
        //效验数据
//        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        log.info(list.toString());
        categoryService.updateBatch(list);
        return new Result();
    }


    @DeleteMapping
    @ApiOperation("逻辑删除")
    @LogOperation("删除")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        // TODO 检查是否存在引用
        categoryService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<CategoryDTO> list = categoryService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, CategoryExcel.class);
    }



}